package labox.innovation.gameserver.util;

import java.io.File;
import java.util.Collection;

import labox.innovation.gameserver.model.L2Object;

/**
 * 为GameServer使用的通用工具类
 * 
 * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
 */
public final class Util {

	public static String getRelativePath(File base, File file) {
		return file.toURI().getPath().substring(base.toURI().getPath().length());
	}

	/**
	 * Return degree value of object 2 to the horizontal line with object 1
	 * being the origin
	 */
	public static double calculateAngleFrom(L2Object obj1, L2Object obj2) {
		return calculateAngleFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
	}

	/**
	 * Return degree value of object 2 to the horizontal line with object 1
	 * being the origin
	 */
	public final static double calculateAngleFrom(int obj1X, int obj1Y, int obj2X, int obj2Y) {
		double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
		if (angleTarget < 0)
			angleTarget = 360 + angleTarget;
		return angleTarget;
	}

	public final static double convertHeadingToDegree(int clientHeading) {
		double degree = clientHeading / 182.044444444;
		return degree;
	}

	public final static int convertDegreeToClientHeading(double degree) {
		if (degree < 0)
			degree = 360 + degree;
		return (int) (degree * 182.044444444);
	}

	public final static int calculateHeadingFrom(L2Object obj1, L2Object obj2) {
		return calculateHeadingFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
	}

	public final static int calculateHeadingFrom(int obj1X, int obj1Y, int obj2X, int obj2Y) {
		double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
		if (angleTarget < 0)
			angleTarget = 360 + angleTarget;
		return (int) (angleTarget * 182.044444444);
	}

	public final static int calculateHeadingFrom(double dx, double dy) {
		double angleTarget = Math.toDegrees(Math.atan2(dy, dx));
		if (angleTarget < 0)
			angleTarget = 360 + angleTarget;
		return (int) (angleTarget * 182.044444444);
	}

	public static double calculateDistance(int x1, int y1, int x2, int y2) {
		double dx = (double) x1 - x2;
		double dy = (double) y1 - y2;

		return Math.sqrt((dx * dx) + (dy * dy));
	}

	public static double calculateDistance(L2Object obj1, L2Object obj2) {
		if (obj1 == null || obj2 == null)
			return 1000000;

		return calculateDistance(obj1.getPosition().getX(), obj1.getPosition().getY(), obj2.getPosition().getX(), obj2.getPosition().getY());
	}

	/**
	 * Capitalizes the first letter of a string, and returns the result.<BR>
	 * (Based on ucfirst() function of PHP)
	 *
	 * @param String str
	 * @return String containing the modified string.
	 */
	public static String capitalizeFirst(String str) {
		str = str.trim();

		if (str.length() > 0 && Character.isLetter(str.charAt(0)))
			return str.substring(0, 1).toUpperCase() + str.substring(1);

		return str;
	}

	/**
	 * Capitalizes the first letter of every "word" in a string.<BR>
	 * (Based on ucwords() function of PHP)
	 *
	 * @param String str
	 * @return String containing the modified string.
	 */
	public static String capitalizeWords(String str) {
		char[] charArray = str.toCharArray();
		String result = "";

		// Capitalize the first letter in the given string!
		charArray[0] = Character.toUpperCase(charArray[0]);

		for (int i = 0; i < charArray.length; i++) {
			if (Character.isWhitespace(charArray[i]))
				charArray[i + 1] = Character.toUpperCase(charArray[i + 1]);

			result += Character.toString(charArray[i]);
		}

		return result;
	}

	/*
	 *  Checks if object is within range, adding collisionRadius
	 */
	public static boolean checkIfInRange(int range, L2Object obj1, L2Object obj2) {
		if (obj1 == null || obj2 == null)
			return false;
		if (obj1.getInstanceId() != obj2.getInstanceId())// 如果不在同一副本
			return false;
		if (obj1.getScene() != obj2.getScene())// 如果不在同一场景
			return false;
		if (range == -1)
			return true; // not limited

		double dx = obj1.getX() - obj2.getX();
		double dy = obj1.getY() - obj2.getY();

		double d = dx * dx + dy * dy;

		return d <= range * range ;

	}

	/*
	 *  Checks if object is within short (sqrt(int.max_value)) radius, 
	 *  not using collisionRadius. Faster calculation than checkIfInRange
	 *  if distance is short and collisionRadius isn't needed.
	 *  Not for long distance checks (potential teleports, far away castles etc)
	 */
	public static boolean checkIfInShortRadius(int radius, L2Object obj1, L2Object obj2) {
		if (obj1 == null || obj2 == null)
			return false;
		if (radius == -1)
			return true; // not limited

		int dx = obj1.getX() - obj2.getX();
		int dy = obj1.getY() - obj2.getY();

		return dx * dx + dy * dy <= radius * radius;

	}

	/**
	 * Returns the number of "words" in a given string.
	 *
	 * @param String str
	 * @return int numWords
	 */
	public static int countWords(String str) {
		return str.trim().split(" ").length;
	}

	/**
	 * Returns a delimited string for an given array of string elements.<BR>
	 * (Based on implode() in PHP)
	 *
	 * @param String[] strArray
	 * @param String strDelim
	 * @return String implodedString
	 */
	public static String implodeString(String[] strArray, String strDelim) {
		String result = "";

		for (String strValue : strArray)
			result += strValue + strDelim;

		return result;
	}

	/**
	 * Returns a delimited string for an given collection of string elements.<BR>
	 * (Based on implode() in PHP)
	 *
	 * @param Collection&lt;String&gt; strCollection
	 * @param String strDelim
	 * @return String implodedString
	 */
	public static String implodeString(Collection<String> strCollection, String strDelim) {
		return implodeString(strCollection.toArray(new String[strCollection.size()]), strDelim);
	}

	/**
	 * Returns the rounded value of val to specified number of digits
	 * after the decimal point.<BR>
	 * (Based on round() in PHP)
	 *
	 * @param float val
	 * @param int numPlaces
	 * @return float roundedVal
	 */
	public static float roundTo(float val, int numPlaces) {
		if (numPlaces <= 1)
			return Math.round(val);

		float exponent = (float) Math.pow(10, numPlaces);

		return (Math.round(val * exponent) / exponent);
	}

	/**
	 * Return amount of adena formatted with "," delimiter
	 * @param amount
	 * @return String formatted adena amount
	 */
	public static String formatAdena(long amount) {
		String s = "";
		long rem = amount % 1000;
		s = Long.toString(rem);
		amount = (amount - rem) / 1000;
		while (amount > 0) {
			if (rem < 99)
				s = '0' + s;
			if (rem < 9)
				s = '0' + s;
			rem = amount % 1000;
			s = Long.toString(rem) + "," + s;
			amount = (amount - rem) / 1000;
		}
		return s;
	}

	/**
	 * 2D版快速求2点间的距离，误差在3.5%左右,一般适合做碰撞检测
	 * 
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @return
	 */
	public static final int FastDistance2D(int x1, int y1, int x2, int y2) {
		int x = Math.abs(x1 - x2);
		int y = Math.abs(y1 - y2);
		int mn = Math.min(x, y);
		return (x + y - (mn >> 1) - (mn >> 2) + (mn >> 4));
	}

	/**
	 * 3D版快速求2点间的距离，误差在8%左右，一般适合做碰撞检测
	 * 
	 * @param fx
	 * @param fy
	 * @param fz
	 * @return
	 */
	public static final float FastDistance3D(float fx, float fy, float fz) {
		int temp;
		int x, y, z;
		// 确保所有的值为正
		x = (int) (Math.abs(fx) * 1024);
		y = (int) (Math.abs(fy) * 1024);
		z = (int) (Math.abs(fz) * 1024);
		// 排序
		if (y < x) {
			temp = x;
			x = y;
			y = temp;
		}
		if (z < y) {
			temp = y;
			y = z;
			z = temp;
		}
		if (y < x) {
			temp = x;
			x = y;
			y = temp;
		}
		int dist = (z + 11 * (y >> 5) + (x >> 2));
		return ((float) (dist >> 10));
	}

}
